Ext.define('Pearlsen.form.PBasic', {
	 extend:'Ext.form.Basic',
	 handlerName:null,
	 datasetName:null,
	 handlerURL:'/query.asp',
	 datasetURL:'/dataset.asp',
	 handlerMethod:'handler',
	 datasetMethod:'handler',
	 afterLoadHandler:null,
	 afterSubmitHandler:null,
	 conditions:[],
	 constructor: function(config) {
		 var me = this;
		 me.handlerName = config.handlerName || me.handlerName;
		 me.datasetName = config.datasetName || me.handlerName;
		 me.handlerURL = config.handlerURL || me.handlerURL;
		 me.datasetURL = config.datasetURL || me.datasetURL;
		 me.handlerMethod = config.handlerMethod || me.handlerMethod;
		 me.datasetMethod = config.datasetMethod || me.datasetMethod;
		 me.afterLoadHandler = config.afterLoadHandler || me.afterLoadHandler;
		 me.afterSubmitHandler = config.afterSubmitHandler || me.afterSubmitHandler;
		 me.callParent([config]);
	 },
		getConditions:function(){
			return this.conditions;
		},
		setConditions:function(conditions){
			this.conditions = conditions || [];
		},
		addCondition:function(condition){
			var tempCondition = this.getCondition(condition.fieldName);
			if(Ext.isEmpty(tempCondition)){
				this.conditions.push(condition);
			}else{
				tempCondition.value = condition.value;
			}
		},
		addConditionIf:function(condition){
			var tempCondition = this.getCondition(condition.fieldName);
			if(Ext.isEmpty(tempCondition)){
				this.conditions.push(condition);
			}
		},
		removeCondition:function(fieldName){
			var index = -1;
			for(var i=0;i<this.conditions.length;i++){
				var c = this.conditions[i];
				if( c.fieldName==fieldName ){
					index = i;
				}
			}
			if( index > -1 ){
				this.conditions.splice(index, 1);
			}
		},
		clearCondition:function(){
			this.conditions.splice(0, this.conditions.length);
		},
		getCondition:function(fieldName){
			for(var i=0;i<this.conditions.length;i++){
				var c = this.conditions[i];
				if( c.fieldName==fieldName ){
					return c;
				}
			}
			return null;
		},
	 
	 
	 
	submit: function(options) {
	    return this.doAction('psubmit', options);
	},
	/**
	 * Shortcut to {@link #doAction do} a {@link Ext.form.action.Load load action}.
	 * @param {Object} options The options to pass to the action (see {@link #doAction} for details)
	 * @return {Ext.form.Basic} this
	 */
	load: function(options) {
	    return this.doAction('pload', options);
	},
	setValues: function(values) {
        var me = this;
        function setVal(fieldId, val) {
            var field = me.findField(fieldId);
            if (field) {
                field.setValue(val);
                if (me.trackResetOnLoad) {
                    field.resetOriginalValue();
                }
            }
        }

        if (Ext.isArray(values)) {
            // array of objects
            Ext.each(values, function(val) {
                setVal(val.id, val.value);
            });
        } else {
            // object hash
            Ext.iterate(values, setVal);
        }
        return this;
    }

});